Code
library(tidyverse)
Tony Duan
January 2, 2024
Sepal.Length Sepal.Width Petal.Length
1 5.1 3.5 1.4
2 4.9 3.0 1.4
3 4.7 3.2 1.3
4 4.6 3.1 1.5
rows = 1, columns = 2, both = c(1, 2)
calculate columns mean
Sepal.Length Sepal.Width Petal.Length
4.825 3.200 1.400
calculate rows mean
lapply() is the speed demon, applying a function to each element of a list or vector and returning a list of results.
Calculate the median of each petal length in a list of lists:
[[1]]
[1] 1.4 1.5 1.6
[[2]]
[1] 4.4 4.5 4.6
sapply() is like lapply(), but it tries to simplify the output. If all results are of the same type (e.g., numeric), it returns a vector instead of a list.
group the Sepal.Length column by the Species column (using iris$Species) and calculate the mean (mean) for each group. The results are stored in sepal_length_by_species
https://www.r-bloggers.com/2024/02/conquering-rs-apply-family-your-guide-to-apply-lapply-sapply-and-tapply/
https://www.youtube.com/watch?v=96-eG79fnms
---
title: "R’s Apply Family"
subtitle: "apply(), lapply(), sapply(), and tapply()"
author: "Tony Duan"
date: "2024-01-03"
categories: [R]
execute:
warning: false
error: false
format:
html:
toc: true
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
---
{width="480"}
```{r}
library(tidyverse)
```
# apply()
```{r}
inputdata=(iris[1:4, 1:3])
inputdata
```
rows = 1, columns = 2, both = c(1, 2)
calculate columns mean
```{r}
column_means <- apply(inputdata, 2, mean)
print(column_means)
```
calculate rows mean
```{r}
column_means <- apply(inputdata, 1, mean)
print(column_means)
```
# lapply()
lapply() is the speed demon, applying a function to each element of a list or vector and returning a list of results.
Calculate the median of each petal length in a list of lists:
```{r}
petal_lengths <- list(c(1.4, 1.5, 1.6), c(4.4, 4.5, 4.6))
petal_lengths
```
```{r}
petal_medians <- lapply(petal_lengths, median)
petal_medians
```
# sapply()
sapply() is like lapply(), but it tries to simplify the output. If all results are of the same type (e.g., numeric), it returns a vector instead of a list.
```{r}
petal_medians <- sapply(petal_lengths, median)
petal_medians
```
# tapply()
group the Sepal.Length column by the Species column (using iris\$Species) and calculate the mean (mean) for each group. The results are stored in sepal_length_by_species
```{r}
sepal_length_by_species <- tapply(iris$Sepal.Length, iris$Species, mean)
print(sepal_length_by_species)
```
# Reference
https://www.r-bloggers.com/2024/02/conquering-rs-apply-family-your-guide-to-apply-lapply-sapply-and-tapply/
https://www.youtube.com/watch?v=96-eG79fnms